package com.example.controller;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.entity.Netfollows;
import com.example.entity.NetfollowsEntity;
import com.example.entity.Students;
import com.example.entity.StudentsEntity;
import com.example.entity.StudentsFindEntity;
import com.example.service.NetfollowsService;
import com.example.service.StudentsService;
import com.example.service.UsersService;
import com.example.util.DownloadUtil;
import com.example.util.ExcelUtil;
import com.example.util.FileUtil;

@RestController
@RequestMapping("/excel")
public class ExcelController {
	
	@Autowired
	private StudentsService studentsService;
	@Autowired
	private NetfollowsService netfollowsService;
	@Autowired
	private UsersService userService;
	
	/**
	 * 导出Excel数据(网络学生)
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/exportWL" , method = RequestMethod.GET, produces = "application/json")
	public void exportWL(HttpServletResponse response) throws Exception{
		ByteArrayOutputStream baos = null;
		OutputStream out = null;
		try {
			// excel数据  实际运用从数据库中查询
			//List<Member> dataList = new ArrayList<Member>();
			//ExcelDataTest data = new ExcelDataTest();
			//Member data = new Member();
			List<StudentsFindEntity> member = studentsService.exportStudent(new StudentsEntity());
			for (int i = 0; i < member.size(); i++) {
				if(member.get(i).getStustatus()!=null && member.get(i).getStustatus()!="")
				if(member.get(i).getStustatus().equals("1")) {
					member.get(i).setStustatus("已录入");
				}else if(member.get(i).getStustatus().equals("2")) {
					member.get(i).setStustatus("已缴费");
				}else if(member.get(i).getStustatus().equals("3")) {
					member.get(i).setStustatus("失败");
				}else {
					member.get(i).setStustatus("");
				}
				if(member.get(i).getIsPay()!=null && member.get(i).getIsPay()!="")
				if(member.get(i).getIsPay().equals("0")) {
					member.get(i).setIsPay("未付款");
				}else if(member.get(i).getIsPay().equals("1")) {
					member.get(i).setIsPay("已付款");
				}else {
					member.get(i).setIsPay("");
				}
			}
			baos = ExcelUtil.exportExcel(member, StudentsFindEntity.class, "学生列表.xlsx", 2);
			DownloadUtil.generalDownload(response, baos, "学生列表.xlsx");
		} catch (Exception e) {
			e.printStackTrace();
		    throw new Exception("导出失败：" + e.getMessage());
		} finally {
			if(baos != null){
				baos.close();
			}
			if(out != null){
				out.close();
			}
		}
	}
	/**
	 * 导出Excel数据(我的学生)
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/exportWD" , method = RequestMethod.GET, produces = "application/json")
	public void exportWD(HttpServletResponse response) throws Exception{
		ByteArrayOutputStream baos = null;
		OutputStream out = null;
		try {
			// excel数据  实际运用从数据库中查询
			//List<Member> dataList = new ArrayList<Member>();
			//ExcelDataTest data = new ExcelDataTest();
			//Member data = new Member();
			StudentsEntity student = new StudentsEntity();
			student.setConsultant("咨询师");
			List<StudentsFindEntity> member = studentsService.exportStudent(student);
			for (int i = 0; i < member.size(); i++) {
				if(member.get(i).getStustatus()!=null && member.get(i).getStustatus()!="")
				if(member.get(i).getStustatus().equals("1")) {
					member.get(i).setStustatus("已录入");
				}else if(member.get(i).getStustatus().equals("2")) {
					member.get(i).setStustatus("已缴费");
				}else if(member.get(i).getStustatus().equals("3")) {
					member.get(i).setStustatus("失败");
				}else {
					member.get(i).setStustatus("");
				}
				if(member.get(i).getIsPay()!=null && member.get(i).getIsPay()!="")
				if(member.get(i).getIsPay().equals("0")) {
					member.get(i).setIsPay("未付款");
				}else if(member.get(i).getIsPay().equals("1")) {
					member.get(i).setIsPay("已付款");
				}else {
					member.get(i).setIsPay("");
				}
			}
			baos = ExcelUtil.exportExcel(member, StudentsFindEntity.class, "学生列表.xlsx", 2);
			DownloadUtil.generalDownload(response, baos, "学生列表.xlsx");
		} catch (Exception e) {
			e.printStackTrace();
		    throw new Exception("导出失败：" + e.getMessage());
		} finally {
			if(baos != null){
				baos.close();
			}
			if(out != null){
				out.close();
			}
		}
	}
	/**
	 * 导出Excel数据(跟踪管理)
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/exportGZ" , method = RequestMethod.GET, produces = "application/json")
	public void exportGZ(HttpServletResponse response) throws Exception{
		ByteArrayOutputStream baos = null;
		OutputStream out = null;
		try {
			// excel数据  实际运用从数据库中查询
			//List<Member> dataList = new ArrayList<Member>();
			//ExcelDataTest data = new ExcelDataTest();
			//Member data = new Member();
			List<Netfollows> member = netfollowsService.findNF(new NetfollowsEntity());
			for (int i = 0; i < member.size(); i++) {
				if(member.get(i).getFollowType()!=null && member.get(i).getFollowType()!="")
				if(member.get(i).getFollowType().equals("1")) {
					member.get(i).setFollowType("电话跟踪");
				}else if(member.get(i).getFollowType().equals("2")) {
					member.get(i).setFollowType("QQ跟踪");
				}else if(member.get(i).getFollowType().equals("3")) {
					member.get(i).setFollowType("微信跟踪");
				}else {
					member.get(i).setFollowType("");
				}
				if(member.get(i).getFollowState()!=null && member.get(i).getFollowState()!="")
				if(member.get(i).getFollowState().equals("1")) {
					member.get(i).setFollowState("已付款");
				}else if(member.get(i).getFollowState().equals("2")) {
					member.get(i).setFollowState("正在跟踪");
				}else if(member.get(i).getFollowState().equals("3")) {
					member.get(i).setFollowState("失败");
				}else {
					member.get(i).setFollowState("");
				}
			}
			baos = ExcelUtil.exportExcel(member, Netfollows.class, "跟踪列表.xlsx", 2);
			DownloadUtil.generalDownload(response, baos, "跟踪列表.xlsx");
		} catch (Exception e) {
			e.printStackTrace();
		    throw new Exception("导出失败：" + e.getMessage());
		} finally {
			if(baos != null){
				baos.close();
			}
			if(out != null){
				out.close();
			}
		}
	}
	
	/**
	 * 导入Excel数据
	 * @param file
	 */
	@RequestMapping(value = "/import", method = RequestMethod.POST)
	public Map<String, Object> importExcel(MultipartFile file) {
		File f = null;
		int id=0;
		try {
			// MultipartFile 转  file
			f = FileUtil.multipartFileToFile(file);
			List<Students> list = ExcelUtil.importExcel(f, Students.class, 2);
			StudentsEntity student=new StudentsEntity();
			for (Students data : list) {
				student.setId(data.getId());
				student.setName(data.getName());
				student.setAge(data.getAge());
				student.setSex(data.getSex());
				student.setPhone(data.getPhone());
				student.setQQ(data.getQQ());
				student.setWeixin(data.getWeixin());
				student.setCreateUser(data.getCreateUser());
				id=userService.getUserId(data.getCreateUser());
				if(id!=0) {
					student.setNetpusherid(id+"");
					id=0;
				}
				// 实际这里是保存数据库操作
				studentsService.addStudents(student);
				student=new StudentsEntity();
			}
			Map<String, Object> map=new HashMap<>();
			map.put("code", 0);
			map.put("msg", "导入成功");
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (f != null) {
				f.delete();
			}
		}
		return null;
	}
}